package com.xk.meeting.service;

import com.xk.meeting.dao.RegformDao;
import com.xk.meeting.dao.RegformDaoImpl;
import com.xk.meeting.entity.Regform;
import com.xk.meeting.entityutils.ApplyList;
import com.xk.meeting.entityutils.ListPage;
import com.xk.meeting.entityutils.RegformList;

import java.util.ArrayList;
import java.util.List;


public class RegformServiceImpl implements RegformService{
    private RegformDao regformDao = new RegformDaoImpl();
    @Override
    public int addRegform(Regform regform) {
        return regformDao.addRegform(regform);
    }

    @Override
    public boolean isExist(int id) {
        int count = regformDao.isExist(id);
        if (count > 0){
            return true;
        }else {
            return false;
        }
    }

    private List<RegformList> getRegformList(int index,int rows) {
        List<RegformList> lists = new ArrayList<>();
        RegformList regformList = null;
        List<Regform> regforms = regformDao.getRegforms(index,rows);
        UserService userService = new UserServiceImpl();
        for (Regform regform : regforms){

            regformList = new RegformList();
            int uid = regform.getUid();

            String username = userService.getNameById(uid);
            String status = userService.getStatus(uid);
            String style = userService.getStatusStyle(uid);

            regformList.setUid(uid);
            regformList.setName(username);
            regformList.setRegform(regform);
            regformList.setStatus(status);
            regformList.setStyle(style);
            lists.add(regformList);
        }
        return lists;
    }

    @Override
    public Regform getRegform(int uid) {
        return regformDao.getRegform(uid);
    }

    @Override
    public int getcount() {
        return regformDao.getCount();
    }

    @Override
    public ListPage<RegformList> getApplyList(int currentPage, int rows) {
        ListPage<RegformList> listPage = new ListPage<>();
        int index = (currentPage-1)*rows;
        List<RegformList> regformList = getRegformList(index,rows);
        int count = regformDao.getCount();

        listPage.setTotalCount(count);
        listPage.setTotalPage(getTotalPage(rows));
        listPage.setList(regformList);
        listPage.setCurrentPage(currentPage);
        listPage.setRows(rows);

        return listPage;
    }

    @Override
    public int getTotalPage(int rows) {
        int count = regformDao.getCount();
        return count % rows == 0 ? count / rows : count / rows + 1;
    }
}
